﻿Public Class frmEgresos
    Dim vectorFormasDePago(10, 2) As String
    Private Sub frmEgresos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        listadoFormasDePago()
    End Sub
    Private Sub listadoFormasDePago()
        Dim i As Integer
        conectar()

        cmd.Connection = con
        cmd.CommandText = "SELECT ID,Nombre FROM tFormasDePago"
        cmd.CommandType = CommandType.Text

        dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                vectorFormasDePago(i, 0) = dr("ID").ToString
                vectorFormasDePago(i, 1) = dr("Nombre").ToString
                cbFormasDePago.Items.Add(vectorFormasDePago(i, 1))
                i += 1
            End While

        End If
        dr.Close()

        desconectar()

    End Sub
    Private Function consultaIDFormasDePago(ByVal texto As String)
        Dim largo, idFormaDePago As Integer
        Dim pagoInArray As String
        largo = vectorFormasDePago.GetLength(0)
        idFormaDePago = 0
        For i = 0 To largo - 1
            pagoInArray = vectorFormasDePago(i, 1)
            If texto = pagoInArray Then
                idFormaDePago = vectorFormasDePago(i, 0)
                Exit For
            End If
        Next
        Return idFormaDePago
    End Function
    Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click
        Dim fa As Integer
        Dim idFormasDePago, lastInsertId As Integer
        Dim documento, fecha, fchVenc, ordenDe As String
        Dim ok As Boolean = True
        If MessageBox.Show("Se creará el egreso. Desea continuar?", "Advertencia", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            idFormasDePago = consultaIDFormasDePago(cbFormasDePago.Text)
            If idFormasDePago > 0 Then
                documento = txtNumeroDocumento.Text
                fecha = dtpFecha.Value.ToShortDateString
                fchVenc = dtpVencimiento.Value.ToShortDateString
                ordenDe = txtALaOrdenDe.Text
                conectar()
                cmd.Connection = con
                cmd.CommandText = "INSERT INTO tPagosEfectuados(Fecha,OrdenDe,Cargo,FechaVencimiento,IDFormaDePago,NumeroDocumento,Total,Glosa) "
                cmd.CommandText += "VALUES ('" + fecha + "', '" + ordenDe + "','" + txtCargo.Text + "', '" + fchVenc + "', '" + idFormasDePago.ToString + "', '" + txtNumeroDocumento.Text + "', '" + txtTotal.Text + "', '" + txtGlosa.Text + "');"
                cmd.CommandType = CommandType.Text
                fa = cmd.ExecuteNonQuery
                desconectar()
                If fa > 0 Then
                    ' EXTRAER ID Ingresado
                    conectar()
                    cmd.Connection = con
                    cmd.CommandText = "SELECT MAX(ID) FROM tPagosEfectuados WHERE Fecha='" + fecha + "' AND OrdenDe='" + ordenDe + "'"
                    cmd.CommandType = CommandType.Text
                    dr = cmd.ExecuteReader
                    If dr.HasRows Then
                        dr.Read()
                        lastInsertId = dr(0)
                    End If
                    dr.Close()
                    desconectar()
                    'SI EL ID ES > 0 ENTONCES CONTINUAR 
                    If lastInsertId > 0 Then
                        txtID.Text = lastInsertId.ToString
                        MessageBox.Show("Se ha creado el egreso. El comprobante se cargará automáticamente.")
                        btnImprimir.Enabled = True
                        btnGuardar.Enabled = False
                        Dim frmVisor As New frmVisorEgreso
                        frmVisor.idPago = txtID.Text
                        frmVisor.Show()
                    End If
                End If
            Else
                MessageBox.Show("Debe seleccionar la forma de pago.")
            End If

        End If
    End Sub

    Private Sub btnImprimir_Click(sender As Object, e As EventArgs) Handles btnImprimir.Click
        Dim frmVisor As New frmVisorEgreso
        frmVisor.idPago = txtID.Text
        frmVisor.Show()
    End Sub
End Class